<template>
	<view class="container">
		<!-- 搜索框和搜索按钮 -->
		<view class="search-bar">
			<input v-model="query" class="search-input" placeholder="搜索" />
			<button class="search-button" @tap="search">搜索</button>
		</view>

		<!-- 历史搜索 -->
		<view class="history-section">
			<view class="history-header">
				<text class="history-title">历史搜索</text>

				<view class="icon-shanchu" @tap="confirmClearHistory">

				</view>
			</view>
			<view class="discover-list">
				<view v-for="item in visibleHistory" :key="item" class="discover-item" @tap="searchFromHistory(item)">
					<text>{{ item }}</text>
				</view>
				<view v-if="history.length > maxVisibleHistory" class="more" @tap="showAllHistory">
					<text>更多...</text>
				</view>
			</view>
		</view>

		<!-- 搜索发现 -->
		<view class="discover-section">
			<text class="discover-title">搜索发现</text>
			<view class="discover-list">
				<view v-for="item in discoverData" :key="item" class="discover-item">
					<text @tap="searchFromHistory(item)">{{ item }}</text>
				</view>
			</view>
		</view>

		<!-- 确认删除对话框 -->
		<modal v-if="showConfirmModal" @confirm="clearHistory" @cancel="hideConfirmModal">
			确认清空历史记录
		</modal>
	</view>
</template>

<script setup>
	import {
		ref,
		computed
	} from 'vue';
	import {
		useSearchStore
	} from '../../stores';

	const searchStore = useSearchStore();
	const query = ref('');
	let maxVisibleHistory = 6;
	const showConfirmModal = ref(false);

	const history = computed(() => searchStore.history);
	const visibleHistory = computed(() => history.value.slice(0, maxVisibleHistory));

	const search = () => {
		if (query.value.trim()) {
			searchStore.addHistory(query.value.trim());
			uni.navigateTo({
				url: `/pages/signup/signup?query=${query.value.trim()}`
			})
		}
	};

	const searchFromHistory = (term) => {
		query.value = term;
		search();
	};

	const confirmClearHistory = () => {
		showConfirmModal.value = true;
	};

	const clearHistory = () => {
		searchStore.clearHistory();
		showConfirmModal.value = false;
	};

	const hideConfirmModal = () => {
		showConfirmModal.value = false;
	};

	const showAllHistory = () => {
		// Implement logic to show all history if needed
		maxVisibleHistory=20
	};

	const discoverData = ref(['按月支付', '500', '近地铁']);
</script>

<style>
	.container {
		padding: 20px;
		background-color: #f8f9fa;
	}

	.search-bar {
		display: flex;
		align-items: center;
		margin-bottom: 20px;
		/* border: 1px solid #ccc; */
		border-radius: 30px;
		background-color: #dfe1e2;
	}

	.search-input {
		flex: 1;
		margin-left: 11px;
		border-radius: 4px;
		font-size: 16px;
	}

	.search-button {
		margin-left: 18px;
		border: none;
		border-radius: 31px;
		background-color: #6e7782;
		color: #fff;
		cursor: pointer;
		font-size: 11px;
	}

	.history-section {
		margin-bottom: 20px;
	}

	.history-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.history-title {
		font-size: 18px;
		font-weight: bold;
	}

	.clear-icon {
		width: 20px;
		height: 20px;
		cursor: pointer;
	}

	.history-list {
		max-height: 120px;
		/* Adjust as needed */
		overflow: hidden;
	}

	.history-item {
		padding: 10px;
		border-bottom: 1px solid #ddd;
		cursor: pointer;
	}

	.history-item:hover {
		background-color: #e9ecef;
	}

	.more {
		text-align: center;
		color: #007bff;
		cursor: pointer;
		padding: 10px;
	}

	.discover-section {
		background-color: #fff;
		padding: 10px;
		border-radius: 4px;
		box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
	}

	.discover-title {
		font-size: 18px;
		font-weight: bold;
		margin-bottom: 10px;
	}

	.discover-list {
		display: flex;
		flex-wrap: wrap;
	}

	.discover-item {
		padding: 9px;
		margin-right: 13px;
		background-color: #f1f1f1;
		border-radius: 12px;
		margin-top: 7px;
		font-size: 12px;
	}
</style>